﻿Public Class CSDNBL

    Private Shared _cn As SQLite.SQLiteConnection

    Public Shared Function GetCn() As SQLite.SQLiteConnection
        If _cn Is Nothing Then
            Dim ta As New CSDN1000DataSetTableAdapters.CSDN1000TableAdapter
            _cn = ta.Connection
        End If

        If _cn.State = ConnectionState.Closed Then
            _cn.Open()
        End If

        Return _cn

    End Function
    Public Shared Function UpdateData(ByVal ds As CSDN1000DataSet, ByRef bCount As Integer) As Boolean
        Try

            Dim ta As New CSDN1000DataSetTableAdapters.CSDN1000TableAdapter
            ta.Adapter.ContinueUpdateOnError = True
            ta.Adapter.UpdateBatchSize = 1
            bCount = ta.Update(ds.CSDN1000)

            Return True

        Catch ex As Exception
            bCount = 0
            Return False
        End Try
    End Function

    Public Shared Function UpdateLastAccessTime(ByVal id As String, ByVal username As String, ByVal blogAddress As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set username='" & username & "',BLOGADDRESS='" & blogAddress & "', LastAccessTime='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"
            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()
        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateUserName(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  UpdateTime='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"
            
            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()

            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateLASTSayHello(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  LASTSayHello='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"
            
            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateLASTFriend(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  LastFriend='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"

            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateLASTLastComment(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  LastComment='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"

            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateLASTLastSetFocus(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  LastSetFocus='" & Now.ToString("yyyyMMddHHmmss") & "' Where ID='" & id & "'"

            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function UpdateLASTLastComment1(ByVal id As String) As Boolean
        Try
            Dim strSql As String = "update CSDN1000 set  LastComment='20220101120101' Where ID='" & id & "'"

            Dim cmd As New SQLite.SQLiteCommand
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try
    End Function

    Public Shared Function GetAllComment() As String()
        Dim lst As New List(Of String)
        Dim strSql As String = "select Content from CSDNComment"
        Dim sqa As New SQLite.SQLiteDataAdapter(strSql, GetCn())
        Dim dt As New DataTable
        If sqa.Fill(dt) > 0 Then
            For idex As Integer = 0 To dt.Rows.Count - 1
                lst.Add(CStr(dt.Rows(idex)("content")))
            Next
        End If

        Return lst.ToArray

    End Function

    Public Enum CSDBCount
        留言个数
        访问空间的个数
        打招呼的个数
        关注的个数
        加为朋友的个数
    End Enum

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="key">例如：20100501</param>
    ''' <param name="CSDBCount"></param>
    ''' <returns>返回个数</returns>
    ''' <remarks></remarks>
    Public Shared Function UpdateCSDBCount(ByVal key As String, ByVal CSDBCount As CSDBCount) As Boolean
        Dim strSql As String = "Update CSDBCount Set "
        Select Case CSDBCount
            Case CSDNBL.CSDBCount.留言个数
                strSql += "CommentCount = CommentCount +1"
            Case CSDNBL.CSDBCount.访问空间的个数
                strSql += "AccessCount = AccessCount +1"
            Case CSDNBL.CSDBCount.打招呼的个数
                strSql += "SayHelloCount = SayHelloCount +1"
            Case CSDNBL.CSDBCount.关注的个数
                strSql += "SetFocusCount = SetFocusCount +1"
            Case CSDNBL.CSDBCount.加为朋友的个数
                strSql += "AddFriendCount = AddFriendCount +1"
        End Select
        strSql += " Where [Date]='" & key + "'"

        Dim cmd As New SQLite.SQLiteCommand
        cmd.CommandText = strSql
        cmd.Connection = GetCn()
        If cmd.ExecuteNonQuery() = 0 Then
            strSql = "Insert into CSDBCount([Date],CommentCount,AccessCount,SayHelloCount,SetFocusCount,AddFriendCount) Values('" & key & "',0,0,0,0,0)"
            cmd.CommandText = strSql
            cmd.Connection = GetCn()
            cmd.ExecuteNonQuery()
        End If

    End Function

    Public Shared Function GetCSDBCount(ByVal key As String, _
                                        ByVal CSDBCount As CSDBCount, _
                                        ByRef count As Integer) As Boolean
        Dim strSql As String = "Select   "
        Select Case CSDBCount
            Case CSDNBL.CSDBCount.留言个数
                strSql += "CommentCount "
            Case CSDNBL.CSDBCount.访问空间的个数
                strSql += "AccessCount "
            Case CSDNBL.CSDBCount.打招呼的个数
                strSql += "SayHelloCount "
            Case CSDNBL.CSDBCount.关注的个数
                strSql += "SetFocusCount "
            Case CSDNBL.CSDBCount.加为朋友的个数
                strSql += "AddFriendCount "
        End Select
        strSql += " From CSDBCount"
        strSql += " Where [Date]='" & key + "'"

        Dim sqa As New SQLite.SQLiteDataAdapter(strSql, GetCn())
        Dim dt As New DataTable
        If sqa.Fill(dt) > 0 Then
            count = CInt(dt.Rows(0)(0))
            Return False
        End If

        count = 0
        Return False

    End Function

End Class
